const _ = require('lodash');

function isObValidate (__ob__, typeName = 'object') {
  return typeof __ob__ === typeName;
}

function getTypeCase (__ob__) {
  if (__ob__ === null) return 'dynamic';

  switch (typeof __ob__) {
    case 'string':
      return 'String';
    case 'number':
      return 'int';
    case 'boolean':
      return 'bool';
    case 'object':
      return 'object';
    default:
      return 'dynamic';
  }
}

function upperCamelCase (key) {
  return _.upperFirst(_.camelCase(key));
}

/**
 * 通过该函数，得到Dart类属性
 * @param {String} key  属性名
 * @param {String} parameter 属性值
 * @param {String} keyClass 经处理的-类名
 * @returns String
 */
function getFinalTypeCase (key, value, keyClass) {
  let literal = getTypeCase(value);
  if (value && literal === 'object') {
    literal = upperCamelCase(keyClass || key);
  }

  if (key.startsWith('?')) {
    return `${literal}? ${key.replace('?', '')}`;
  } else {
    return `${literal} ${key}`;
  }
}

function isDartType (typename) {
  return ['String', 'int', 'bool', 'dynamic'].includes(typename);
}

function transformString (textStr = '') {
  try {
    return JSON.parse(textStr);
  } catch (error) {
    return error;
  }
}

function isEmptyObject (__ob__) {
  if (Array.isArray(__ob__)) {
    return false;
  }
  return isObValidate(__ob__) && !Object.keys(__ob__).length;
}

module.exports = {
  isObValidate,
  getFinalTypeCase,
  getTypeCase,
  upperCamelCase,
  isDartType,
  transformString,
  isEmptyObject
}
